/*
 * TSessionPanel.java
 *
 * Created on April 23, 2007, 11:15 AM
 */

package cotex.registry;

import cotex.*;

/**
 *
 * @author  Ming
 */
public class TSessionPanel extends javax.swing.JPanel {
    
    private TNode mNode;
    
    /** Creates new form TSessionPanel */
    public TSessionPanel(TNode node) {
        
        initComponents();
        
        mNode = node;
        
        mSessionTree.setModel( ((TRegistryNodeModel)mNode.getModel()).data.treeModel);
        
        javax.swing.event.TreeModelListener listener = 
            new javax.swing.event.TreeModelListener() {
            
            public void treeNodesChanged(javax.swing.event.TreeModelEvent e) {
                expandTreeNodes();}
            
            public void treeNodesInserted(javax.swing.event.TreeModelEvent e) {
                expandTreeNodes();}
            
            public void treeNodesRemoved(javax.swing.event.TreeModelEvent e) {
                expandTreeNodes();}
            
            public void treeStructureChanged(javax.swing.event.TreeModelEvent e) {
                expandTreeNodes();}
        };
        
        mSessionTree.getModel().addTreeModelListener(listener);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        mSessionTree = new javax.swing.JTree();

        setLayout(new java.awt.BorderLayout());

        setName("Sessions");
        jScrollPane1.setViewportView(mSessionTree);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

    }// </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree mSessionTree;
    // End of variables declaration//GEN-END:variables
    
    private void expandTreeNodes() {
        
        //expandAll(
        //    mSessionTree,
        //    new javax.swing.tree.TreePath( mSessionTree.getModel().getRoot() ),
        //    true);
        
    }
    
    private void expandAll(javax.swing.JTree tree, javax.swing.tree.TreePath parent, boolean expand) {
        
        // Traverse children
        javax.swing.tree.TreeNode node = (javax.swing.tree.TreeNode)parent.getLastPathComponent();
        
        if (node.getChildCount() >= 0) {
            
            for (java.util.Enumeration e = node.children(); e.hasMoreElements(); ) {
                
                javax.swing.tree.TreeNode n = (javax.swing.tree.TreeNode)e.nextElement();
                javax.swing.tree.TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }
    
        // Expansion or collapse must be done bottom-up
        if (expand) {
            
            tree.expandPath(parent);
            
        } else {
            
            tree.collapsePath(parent);
            
        }
    }
}
